package com.mysiteforme.admin.controller.saleMan;

import com.mysiteforme.admin.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricitySaleMan;
import com.mysiteforme.admin.service.ElectricitySaleManService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 销售人员添加  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-18
 */
@Controller
@RequestMapping("/admin/electricitySaleMan")
public class ElectricitySaleManController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricitySaleManController.class);

    @Autowired
    private ElectricitySaleManService electricitySaleManService;

    @GetMapping("list")
    @SysLog("跳转销售人员添加列表")
    public String list() {
        return "/admin/electricitySaleMan/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求销售人员添加列表数据")
    public LayerData<ElectricitySaleMan> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                              @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                              ServletRequest request) {
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricitySaleMan> layerData = new LayerData<>();
        EntityWrapper<ElectricitySaleMan> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", false);
        wrapper.orderBy("create_date", false);
        if (!map.isEmpty()) {
            String saleServiceNumber = (String) map.get("saleServiceNumber");
            if (StringUtils.isNotBlank(saleServiceNumber)) {
                wrapper.like("sale_service_number", saleServiceNumber);
            } else {
                map.remove("saleServiceNumber");
            }

            String saleManName = (String) map.get("saleManName");
            if (StringUtils.isNotBlank(saleManName)) {
                wrapper.like("sale_man_name", saleManName);
            } else {
                map.remove("saleManName");
            }

        }
        Page<ElectricitySaleMan> pageData = electricitySaleManService.selectPage(new Page<>(page, limit), wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增销售人员添加页面")
    public String add() {
        return "/admin/electricitySaleMan/add";
    }

    @PostMapping("add")
    @SysLog("保存新增销售人员添加数据")
    @ResponseBody
    public RestResponse add(ElectricitySaleMan electricitySaleMan) throws Exception {
        if (StringUtils.isBlank(electricitySaleMan.getSaleServiceNumber())) {
            return RestResponse.failure("售后服务编号不能为空");
        }

        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("sale_service_number", electricitySaleMan.getSaleServiceNumber());
        entityWrapper.eq("del_flag", "0");
        List<ElectricitySaleMan> list = electricitySaleManService.selectList(entityWrapper);
        if (list != null && list.size() > 0) {
            return RestResponse.failure("该销售编码已存在");
        }

        // 存放在二维码中的内容
        String number = electricitySaleMan.getSaleServiceNumber();//销售服务编码
        String realPath = System.getProperties().getProperty("user.home");
        // 二维码路径
        String content = Constants.URL + number;
        //名称
        String name = "image.jpg";
        File file = QRCodeUtils.testEncode(realPath, content, name);
        if (file != null) {
            // 文档上传oss
            String pox = name.substring(name.lastIndexOf(".") + 1);
            InputStream inputStream = new FileInputStream(file);
            Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "YDYL/SALEMAN/");
            electricitySaleMan.setCode(uploadByInputStream.get("path").toString());
        }
        boolean bool = electricitySaleManService.insert(electricitySaleMan);
        if (bool) {
            return RestResponse.success();
        }
        return RestResponse.failure("新增失败");
    }

    @GetMapping("edit")
    @SysLog("跳转编辑销售人员添加页面")
    public String edit(Long id, Model model) {
        ElectricitySaleMan electricitySaleMan = electricitySaleManService.selectById(id);
        model.addAttribute("electricitySaleMan", electricitySaleMan);
        return "/admin/electricitySaleMan/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑销售人员添加数据")
    public RestResponse edit(ElectricitySaleMan electricitySaleMan) throws Exception {
        if (null == electricitySaleMan.getId() || 0 == electricitySaleMan.getId()) {
            return RestResponse.failure("ID不能为空");
        }
        if (StringUtils.isBlank(electricitySaleMan.getSaleServiceNumber())) {
            return RestResponse.failure("售后服务编号不能为空");
        }
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("sale_service_number", electricitySaleMan.getSaleServiceNumber());
        entityWrapper.eq("del_flag", "0");
        ElectricitySaleMan saleMan = electricitySaleManService.selectOne(entityWrapper);

        if (saleMan != null && !saleMan.getId().equals(electricitySaleMan.getId())) {
            return RestResponse.failure("该销售编码已存在");
        } else {
            // 存放在二维码中的内容
            String number = electricitySaleMan.getSaleServiceNumber();//销售服务编码
            String realPath = System.getProperties().getProperty("user.home");
            // 二维码路径
            String content = Constants.URL + number;
            //名称
            String name = "image.jpg";
            File file = QRCodeUtils.testEncode(realPath, content, name);
            if (file != null) {
                // 文档上传oss
                String pox = name.substring(name.lastIndexOf(".") + 1);
                InputStream inputStream = new FileInputStream(file);
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "YDYL/SALEMAN/");
                electricitySaleMan.setCode(uploadByInputStream.get("path").toString());
            }

            electricitySaleManService.updateById(electricitySaleMan);
            return RestResponse.success();
        }
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除销售人员添加数据")
    public RestResponse delete(@RequestParam(value = "id", required = false) Long id) {
        if (null == id || 0 == id) {
            return RestResponse.failure("ID不能为空");
        }
        ElectricitySaleMan electricitySaleMan = electricitySaleManService.selectById(id);
        electricitySaleMan.setDelFlag(true);
        electricitySaleManService.updateById(electricitySaleMan);
        return RestResponse.success();
    }

}
